package psi.services;

import java.util.ArrayList;
import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

import psi.common.PSIConst;
import psi.common.Result;

/**
 * 权限 Service
 * 
 * @author 李静波
 *
 */
public class PermissionService extends PSIBaseService {

	public List<Record> roleList() {
		UserService us = new UserService();
		String dataOrg = us.getLoginUserDataOrg();

		String sql = "";
		ArrayList<Object> qp = new ArrayList<Object>();
		sql += " select id, name from t_role ";
		if (!us.isAdmin()) {
			if (!PSIConst.ALL_DATA_ORG.equals(dataOrg)) {
				sql += " where left(data_org, " + dataOrg.length() + ") = ?";
				qp.add(dataOrg);

				sql += " and (length(data_org) >= ?)";
				qp.add(dataOrg.length());
			}
		}
		sql += " order by convert(name USING gbk) collate gbk_chinese_ci";

		return Db.find(sql, qp.toArray());
	}

	public List<Record> permissionList(String roleId) {
		String sql = "";
		sql += " select p.id, p.name ";
		sql += " from t_role r, t_role_permission rp, t_permission p ";
		sql += " where r.id = rp.role_id and r.id = ? and rp.permission_id = p.id ";
		sql += " order by convert(p.name USING gbk) collate gbk_chinese_ci";

		List<Record> result = Db.find(sql, roleId);

		for (Record r : result) {
			sql = "";
			sql += " select data_org ";
			sql += " from t_role_permission_dataorg ";
			sql += " where role_id = ? and permission_id = ? ";
			List<Record> dataOrgList = Db.find(sql, roleId, r.getStr("id"));
			if (dataOrgList.isEmpty()) {
				r.set("dataOrg", "[全部数据]");
			} else {
				String dataOrg = "";
				for (int i = 0; i < dataOrgList.size(); i++) {
					Record dataOrgItem = dataOrgList.get(i);
					if (i > 0) {
						dataOrg += ";";
					}

					String item = dataOrgItem.getStr("data_org");
					if ("*".equals(item)) {
						dataOrg += "[全部数据]";
					} else if ("#".equals(item)) {
						dataOrg += "[用户自己]";
					} else {
						dataOrg += item;
					}
				}

				r.set("dataOrg", dataOrg);
			}
		}

		return result;
	}

	public List<Record> userList(String roleId) {
		List<Record> result = new ArrayList<Record>();

		String sql = "";
		sql += " select u.id, u.login_name, u.name, org.full_name ";
		sql += " from t_role r, t_role_user ru, t_user u, t_org org ";
		sql += " where r.id = ru.role_id and r.id = ? and ru.user_id = u.id and u.org_id = org.id ";
		sql += " order by convert(org.full_name USING gbk) collate gbk_chinese_ci";

		List<Record> data = Db.find(sql, roleId);
		for (Record item : data) {
			Record user = new Record();
			user.set("id", item.getStr("id"));
			user.set("loginName", item.getStr("login_name"));
			user.set("name", item.getStr("name"));
			user.set("orgFullName", item.getStr("full_name"));

			result.add(user);
		}

		return result;
	}

	public List<Record> selectPermission(String idList) {
		return emptyList();
	}

	public List<Record> selectUsers(String idList) {
		return emptyList();
	}

	public List<Record> dataOrgList(String roleId, String permissionId) {
		String sql = "";
		sql += " select data_org ";
		sql += " from t_role_permission_dataorg ";
		sql += " where role_id = ? and permission_id = ? ";
		List<Record> data = Db.find(sql, roleId, permissionId);

		List<Record> result = new ArrayList<Record>();

		if (data.isEmpty()) {
			result.add(new Record().set("dataOrg", "*").set("orgFullName", "[全部数据]"));
		} else {
			for (Record r : data) {
				String dataOrg = r.getStr("data_org");
				String orgFullName = "";
				if ("*".equals(dataOrg)) {
					orgFullName = "[全部数据]";
				} else if ("#".equals(dataOrg)) {
					orgFullName = "[用户自己]";
				} else {
					sql = " select full_name from t_org where data_org = ? ";
					orgFullName = Db.queryStr(sql, dataOrg);
					if (orgFullName == null) {
						sql = "";
						sql += " select concat(o.full_name, ?, u.name) ";
						sql += " from t_user u, t_org o ";
						sql += " where u.data_org = ? and u.org_id = o.id limit 1 ";
						orgFullName = Db.queryStr(sql, "\\", dataOrg);
					}
				}

				Record rd = new Record().set("dataOrg", dataOrg).set("orgFullName", orgFullName);
				result.add(rd);
			}
		}

		return result;
	}

	public Result editRole(Record params) {
		return todo();
	}
}
